/**
 * 
 */
package com.keggview.application.xml.loader;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.keggview.application.datatypes.Alt;
import com.keggview.application.datatypes.Base;
import com.keggview.application.datatypes.Component;
import com.keggview.application.datatypes.Entry;
import com.keggview.application.datatypes.Graphics;
import com.keggview.application.datatypes.Pathway;
import com.keggview.application.datatypes.Product;
import com.keggview.application.datatypes.Reaction;
import com.keggview.application.datatypes.Relation;
import com.keggview.application.datatypes.Substrate;
import com.keggview.application.datatypes.Subtype;

/**
 * @author Pablo
 *
 */
public class ObjectFactory {

	public static Base createObject(Class<?> clazz, Object[] params) {
		Base retVal = null;
		ArrayList<Class<?>> sigClass = new ArrayList<Class<?>>();
		for (int i = 0; i < params.length; i++) {
			sigClass.add(params[i].getClass());
		}
		Class<?>[] sigs = sigClass.toArray(new Class<?>[params.length]);
		
		try {
			Constructor<?> constructor =  clazz.getConstructor(sigs);
			retVal = (Base) constructor.newInstance(params);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return retVal;
	}
	
	@SuppressWarnings("unchecked")
	public static ArrayList<? extends Base> lookFor(Element element, String name) {
		ArrayList<Base> list = new ArrayList<Base> ();
		List<Element> objElements = element.elements(name);
		Class<?> clazz = getClassEntity(name);
		for (Element item : objElements) {
			Object[] tempObj = { item };
			Base newObj = ObjectFactory.createObject(clazz, tempObj);
			list.add(newObj);
		}
		return list;
	}
	
	public static Class<?> getClassEntity(String name) {
		Class<?> retVal = null;
    	if (name.equals("alt")) {
    		retVal = Alt.class;
    	}
    	if (name.equals("component")) {
    		retVal = Component.class;
    	}
    	if (name.equals("entry")) {
    		retVal = Entry.class;
    	}
    	if (name.equals("graphics")) {
    		retVal = Graphics.class;
    	}
    	if (name.equals("pathway")) { 
    		retVal = Pathway.class;
    	}
    	if (name.equals("product")) { 
    		retVal = Product.class;
    	}
    	if (name.equals("reaction")) { 
    		retVal = Reaction.class;
    	}
    	if (name.equals("relation")) { 
    		retVal = Relation.class;
    	}
    	if (name.equals("substrate")) {
    		retVal = Substrate.class;
    	}
    	if (name.equals("subtype")) {
    		retVal = Subtype.class;
    	}
    	return retVal;
	}
}
